package org.jeecg.modules.imm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.imm.service.impl.ImmRecordServiceImpl;
import org.jeecg.modules.scm.api.ScmCommonCloudApi;
import org.jeecg.modules.scm.api.ScmFinaApplyApi;
import org.jeecg.modules.scm.api.ScmInvItemRsApi;
import org.jeecg.modules.scm.common.util.HttpRequest;
import org.jeecg.modules.scm.dto.BankDataDto;
import org.jeecg.modules.scm.dto.EditInvItemRsDto;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "资金方报文")
@RestController
@RequestMapping("/InvMessage")
@Slf4j
public class ImmBaseCloudController {

    /**资金方项目关联*/
    @Autowired
    private ScmInvItemRsApi scmInvItemRsApi;
    /**融资申请Feign*/
    @Autowired
    private ScmFinaApplyApi scmFinaApplyApi;
    /**业务模块cloudAPI*/
    @Autowired
    private ScmCommonCloudApi scmCommonCloudApi;

    /**记录报文服务*/
    @Autowired
    private ImmRecordServiceImpl immRecordService;


    /***/
    @AutoLog(value = "资金方关联关系报文接收")
    @ApiOperation(value = "资金方关联关系报文接收", notes = "s资金方关联关系报文接收")
    @PostMapping(value = "investorRelationReceive")
    public Result<?> investorRelationReceive(@RequestBody EditInvItemRsDto editInvItemRsDto){
        scmInvItemRsApi.edit(editInvItemRsDto);
        return Result.OK("更新成功");
    }

    /**
     * 融资申请审批报文-接收
     * */
    @PostMapping(value = "/finaApplyReceive")
    public Result<?> finaApplyReceive(@RequestBody(required = false) MultipartFile contractTemplate,HttpServletRequest req){

        System.out.println(contractTemplate);
//        System.out.println(1);
//        Result<?> result = scmFinaApplyApi.bankReceive();
//
//        if(result==null||result.getResult()==null){
//            return Result.error("审批失败");
//        }
//        return Result.OK("审批成功");
//        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
//        params.add("type",req.getParameter("type"));
//        params.add("data",req.getParameter("data"));
//        params.add("result",req.getParameter("result"));
//        params.add("uploadContract",req.getParameter("uploadContract"));
//
        BankDataDto bankDataDto = new BankDataDto();
        bankDataDto.setType(req.getParameter("type"));
        bankDataDto.setData(req.getParameter("data"));
        bankDataDto.setResult(req.getParameter("result"));
        bankDataDto.setUploadContract(req.getParameter("uploadContract"));
//        Part template = null;
//        if(bankDataDto.getType().equals("审批")){
//            try {
//                template = req.getPart("contractTemplate");
////                params.add("contractTemplate",req.getParameter("contractTemplate"));
//            } catch (Exception e) {
//                throw new JeecgBootException("失败");
//            }
//        }
//
        Result<?> bankReceive = null;
        System.out.println(bankDataDto);
        if(bankDataDto.getType().equals("审批")){
            bankReceive = scmFinaApplyApi.bankReceive(JSONObject.toJSONString(bankDataDto),contractTemplate);
        }else{
            bankReceive = scmFinaApplyApi.bankLendReceive(JSONObject.toJSONString(bankDataDto));
        }
        return bankReceive;


    }


    /**融资申请报文-发送*/
    @PostMapping(value = "/finaApplySend")
    public Result<?> finaApplySend(@RequestParam String url,@RequestParam MultiValueMap<String, Object> params){
        String res = HttpRequest.PostInfomation(url, params);
        System.out.println(res);
        if(res==null){
            return Result.error("发送失败");
        }
        return Result.OK("发送成功");
    }

    /**
     * @title 余额变动报文-接收
     * @description
     * @author Zhou Ruiji
     * @updateTime 2021/12/13 23:30
     * @throws
     */
    public Result<?> balanceChangeAccept(@RequestBody JSONObject jsonObject) {
        ScmFinaContract scmFinaContract = JSON.parseObject(jsonObject.toJSONString(), ScmFinaContract.class);
        Result<?> result = scmCommonCloudApi.finaContractEdit(scmFinaContract);
        return result;
    }

    /**
     * @title 结清报文-接收
     * @description
     * @author Zhou Ruiji
     * @updateTime 2021/12/13 23:34
     * @throws
     */
    public Result<?> settleAccept(@RequestBody JSONObject jsonObject) {
        ScmFinaContract scmFinaContract = JSON.parseObject(jsonObject.toJSONString(), ScmFinaContract.class);
        Result<?> result = scmCommonCloudApi.finaContractEdit(scmFinaContract);
        return result;
    }


    }
